package service

import (
	"egin/app/models"
	"egin/app/request"
	"egin/pkg/captcha"
	"egin/pkg/database"
	"egin/pkg/utils"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

//判断端密码是否正确
func Login(u request.Loegintruct) (res bool, userInter *models.Member) {

	var member models.Member
	err := database.Db().Where("username = ?", u.Username).Or("email = ?", u.Username).Or("mobile = ?", u.Username).First(&member).Error

	if err != nil {
		return false, &member
	}

	res = utils.PasswordVerify(u.Password, member.Password)

	return res, &member

}

//判断密码是否正确
func Unlock(c *gin.Context, param request.UnlockStruct) bool {
	session := sessions.Default(c)
	uid := session.Get("id")

	var member models.Member
	err := database.Db().Where("id = ?", uid.(string)).First(&member).Error

	if err != nil {
		return false
	}

	res := utils.PasswordVerify(param.Password, member.Password)

	return res

}

//  验证 验证码是否正确
// captchaId: 存于session中
// verifyValue: 客户端发来的验证码
func VerfiyCaptcha(verifyValueId, verifyValue string) bool {

	verifyResult := captcha.CaptVerify(verifyValueId, verifyValue)
	return verifyResult
}
